package com.woniuxy.woniuhis.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.DocAdvice;
import com.woniuxy.woniuhis.exception.DocAdviceServiceException;
import com.woniuxy.woniuhis.mapper.DocAdviceMapper;
import com.woniuxy.woniuhis.service.DocAdviceService;
import com.woniuxy.woniuhis.util.UUIDUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class DocAdviceServiceImpl implements DocAdviceService {
    @Autowired
    private DocAdviceMapper docAdviceMapper;

    @Override
    public void addDocAdvice(DocAdvice docAdvice) {
        try {
            if (docAdvice.getDocAdviceId() == null) {
                docAdvice.setDocAdviceId(UUIDUtil.uuid32());
            }
            docAdviceMapper.insertDocAdvice(docAdvice);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DocAdviceServiceException("服务维护中");
        }
    }

    @Override
    public void delDocAdviceById(String docAdviceId) {
        try {
            if (!StringUtils.isEmpty(docAdviceId)) {
                docAdviceMapper.deleteDocAdviceById(docAdviceId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new DocAdviceServiceException("服务维护中");
        }
    }

    @Override
    public void modifyDocAdvice(DocAdvice docAdvice) {
        try {
            if (docAdvice != null) {
                docAdviceMapper.updateDocAdvice(docAdvice);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new DocAdviceServiceException("服务维护中");
        }
    }

    @Override
    public DocAdvice findDocAdviceById(String docAdviceId) {
        try {
            if (!StringUtils.isEmpty(docAdviceId)) {
                return docAdviceMapper.selectDocAdviceById(docAdviceId);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new DocAdviceServiceException("服务维护中");
        }
    }

    @Override
    public PageInfo<DocAdvice> findDocAdviceListByCondition(DocAdvice condition, Integer pageNum, Integer pageSize) {
        try {
            if (pageNum == null) {
                pageNum = 0;
            }
            if (pageSize == null || pageSize <= 0) {
                //默认返回五条数据
                pageSize = 5;
            }
            return new PageInfo<DocAdvice>(docAdviceMapper.selectDocAdviceListByCondition(condition, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            throw new DocAdviceServiceException("服务维护中");
        }
    }
}
